package jsyf.oa.web.app;

import com.alibaba.fastjson.JSONObject;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.app.UpdateFacade;

@Api(value = "app更新接口", description = "app更新接口")
@Controller
@RequestMapping("/app/update")
public class UpdateController {
    @Resource
    private UpdateFacade updateFacade;


    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "新增更新",
            notes = "新增更新 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{\n" +
            "  \"versionCode\": 1,\n" +
            "  \"versionName\": \"1.0\",\n" +
            "  \"apkSize\": \"7.5MB\",\n" +
            "  \"remark\": \"新增说明文档;修改了一些BUG\",\n" +
            "  \"forceFlag\": 0,\n" +
            "  \"ignoreFlag\": 0,\n" +
            "  \"userIds\": 指定更新,id逗号隔开,\n" +
            "  \"roleIds\":  指定更新,id逗号隔开,\n" +
            "  \"departIds\": 指定更新,id逗号隔开,\n" +
            "  \"companyIds\": 指定更新,id逗号隔开\n" +
            "}", dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = updateFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getVersion.json", method = RequestMethod.GET)
    public void getVersion(HttpServletResponse response) {
        JSONObject records = updateFacade.getVersion();
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }
}
